import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        List<List<Integer>> result = new ArrayList<>();
        // 最后的和等于0
        Arrays.sort(nums);
        for (int i = 0; i < n;) {
            int flag = -nums[i];
            int left=i+1,right=n-1;
            while (left<right){
                int sum = nums[left]+nums[right];
                if(sum<flag) left++;
                else if(sum>flag) right--;
                else {
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    result.add(list);
                    left++;right--;
                    while (left<right&&nums[left-1]==nums[left]) left++;
                    while (left<right&&nums[right+1]==nums[right]) right--;
                }
            }
            i++;
            while (i<n&&nums[i-1]==nums[i]) i++;
        }
        return result;
    }
}